﻿using EFEntity;
using ISerives;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Model;
using Newtonsoft.Json;
using Serives;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace HRDB.Controllers
{
    public class powerController : Controller
    {
        private readonly IsfServices sfSerives;
        private readonly IusersServices userSerives;
        private readonly MyDbContext myDbContext; 
        private readonly IpowersServices powerSerives;

        public powerController(IsfServices sfSerives,
            IusersServices userSerives,
            IpowersServices powerSerives,
            MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
            this.powerSerives = powerSerives;
            this.sfSerives = sfSerives;
            this.userSerives = userSerives;
        }
        private async Task ssf()
        {
            List<sf> list = await sfSerives.Select();
            SelectList sl = new SelectList(list, "user_sf", "user_sf");
            ViewBag.s = sl;
        }
        //用户管理
        #region
      
        public IActionResult ququq(string idl, string id)
        {
            string sql = "";
            if (id == null)
            {
                //查询父级
                sql = $@"select q.id, q.text, q.state,q.pid,q.qurl,qr.QName,
                             case
                             when qr.QName is null then 0
                             else 1
                            end as checked
                            from power q
                            left join(select QName from dbo.QuanXian where Role={idl}) qr on q.id=qr.QName
                            where q.pid=0";
            }
            else
            {
                //查询父级里面的子集
                sql = $@"select q.id, q.text, q.state,q.pid,q.qurl,qr.QName,
                             case
                             when qr.QName is null then 0
                             else 1
                            end as checked
                            from power q
                            left join(select QName from dbo.QuanXian where Role={idl}) qr on q.id=qr.QName
                            where q.pid={id}";
            }
            DataTable dt = BaseDao.GetData(sql, "powerController");

            return Content(JsonConvert.SerializeObject(dt));
        }
        public async Task<IActionResult> user_list()
        {
            await ssf();
            ViewData.Model = await userSerives.Select();
            return View();
        }
        [HttpGet]
        public async Task<IActionResult> user_edit(int  id)
        {
            await ssf();
            users list = await userSerives.Up(id);
            return View(list);
        }
        [HttpPost]
        public async Task<IActionResult> user_edit(users t)
        {
           
            int num = await userSerives.Update(t);
            if (num > 0)
            {
                return RedirectToAction("success");
            }
            else
            {
                return View(t);
            };
        }
        public IActionResult success()
        {
            return View();
        }
        [HttpGet]
        public async Task<IActionResult> user_add()
        {
            await ssf();
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> user_add(users t)
        {
            int num = await userSerives.Add(t);
            if (num > 0)
            {
                return RedirectToAction("success");
            }
            else
            {
                await ssf();
                return View(t);
            }
        }
        public async Task<IActionResult> user_d(int id)
        {
            int num = await userSerives.Delete(id);
            if (num > 0)
            {
                return RedirectToAction("user_list");
            }
            else
            {
                return View();
            }
        }
        #endregion
        //角色管理
        #region
        public  IActionResult right_add()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> right_add(Esf e)
        {
            int num = await sfSerives.Add(e);
            if (num > 0)
            {
                return RedirectToAction("right_list");
            }
            else
            {
                return View(e);
            }
           
        }
        public async Task<IActionResult> right_d(int id)
        {
            int num = await sfSerives.Del(id);
            if (num > 0)
            {
                return RedirectToAction("right_list");

            }
            else
            {
                return  View();
            }
        }
        public async Task<IActionResult> right_list()
        {
            ViewData.Model = await sfSerives.Select();
            return View();
        }
        //拿id跳转修改页面
        public async Task<IActionResult> right_list_information(int id)
        {
            Esf esf = await sfSerives.Up(id);
            return View(esf);
        }
        public async Task<IActionResult> updat(int idl,string id)
        {
            List<power> esf = await powerSerives.Select(idl,id);
            return Content(JsonConvert.SerializeObject(esf));
        }
        [HttpPost]
        public  async Task<IActionResult> addquanxianjs(int sid ,string ids)
        {
            int msg = await powerSerives.Del(sid);
            return View();
        }
        public static int result;
        QuanXianDao d = new QuanXianDao();
        public async Task<IActionResult> update(string ids, int sid)//修改树形菜单权限  ids:获取所有节点  sid:用户id
        {
            int rid = Convert.ToInt32(sid);
            int re = await d.Del(rid);//先删除，在添加

            string[] sArray = Regex.Split(ids, ",", RegexOptions.IgnoreCase);//根据逗号截取父节点
            foreach (string i in sArray)//循环遍历每个传过来的id
            {
                Model.QuanXian rr = new Model.QuanXian()
                {
                    QName = Convert.ToInt32(i),
                    Role = Convert.ToInt32(sid)
                };

                result = await d.Add(rr);//循环遍历所有进行一一添加
            }

            return Content(JsonConvert.SerializeObject(result));
        }
        //查询树状菜单
        public async Task<IActionResult> Editequanxianjs(int idl, string id)
        {
            List<power> esf = await powerSerives.Select(idl, id);
            return Content(JsonConvert.SerializeObject(esf));
        }
        //修改树状菜单
        public async Task<IActionResult> up(int id,string user_sf, string user_ms, string user_sfny)
        {
            Esf dd = new Esf()
            {
                id = id,
                user_sf = user_sf,
                user_ms = user_ms,
                user_sfny = user_sfny
            };
            int num = await sfSerives.Update(dd);
            return Content(JsonConvert.SerializeObject(num));
        }
    




        #endregion
    }

}
